import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:get/get.dart';

import 'controller.dart';

class SqfLiteForm extends StatefulWidget {
  final formKey;

  const SqfLiteForm({Key? key,required this.formKey}) : super(key: key);

  @override
  SqfLiteFormState createState() => SqfLiteFormState();
}

class SqfLiteFormState extends State<SqfLiteForm> {
  bool hasSubmited = false;

  String? textFieldTitle = "", textFieldBody = "";

  final controller = Get.find<SqfLiteController>();

  @override
  Widget build(BuildContext context) {
    return Form(
        autovalidateMode:
            hasSubmited ? AutovalidateMode.always : AutovalidateMode.disabled,
        key: widget.formKey,
        onChanged: () {
          print('form changed');
          (widget.formKey.currentState as FormState).save();
        },
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            SizedBox(height: 50),
            TextFormField(
              decoration: InputDecoration(
                  hintText: "Please input title",
                  prefixIcon: Icon(Icons.lock),
                  labelText: 'title',
                  border: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.blue))),
              onSaved: (value) {
                setState(() {
                  textFieldTitle = value;
                });
              },
              //校验密码
              validator: (v) {
                return v!.trim().length > 0 ? null : "title不能为空";
              },
            ),
            SizedBox(height: 30),
            TextFormField(
              decoration: InputDecoration(
                  hintText: "Please input body",
                  prefixIcon: Icon(Icons.lock),
                  labelText: 'body',
                  border: OutlineInputBorder(
                      borderSide: BorderSide(color: Colors.blue))),
              onSaved: (value) {
                setState(() {
                  textFieldBody = value;
                });
              },
              //校验密码
              validator: (v) {
                return v!.trim().length > 0 ? null : "body不能为空";
              },
            ),
            SizedBox(height: 50),
            ConstrainedBox(
                constraints: BoxConstraints.expand(height: 36),
                child: ElevatedButton(
                    onPressed: () {
                      if (!hasSubmited) {
                        setState(() {
                          hasSubmited = true;
                        });
                      }
                      final formState = (widget.formKey.currentState as FormState);
                      formState.save();
                      if (formState.validate()) {
                        //通过验证 提交数据

                        setState(() {
                          hasSubmited = false;
                          controller.slidingUpPanelController.close();
                          formState.reset();
                        });
                      }
                    },
                    child: Text("提交"))),
            SizedBox(height: 30),
            ConstrainedBox(
              constraints: BoxConstraints.expand(height: 36),
              child: ElevatedButton(
                  onPressed: () {
                    (widget.formKey.currentState as FormState).reset();
                  },
                  child: Text("重置")),
            ),
          ],
        ));
  }
}
